package com.retail.member.controller;

import com.alibaba.fastjson.JSONObject;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.entity.result.Result;
import com.retail.member.client.UserClient;
import com.retail.member.entity.request.MemberUserRequest;
import com.retail.member.entity.vo.RetailMultiTeamResVo;
import com.retail.member.entity.vo.RetailTeamResVo;
import com.retail.order.entity.vo.SysAdminVerifyStasticResVo;
import com.retail.proxy.entity.request.SysAdminApplyProxyRequest;
import com.retail.system.entity.request.AdminRequest;
import com.retail.system.entity.vo.AdminVo;
import com.retail.team.entity.request.RetailTeamAddSpRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(value = "登录页面API", tags = "登录页面API")
@RestController
@RequestMapping("api/user")
public class UserApiController {

    private final UserClient userClient;

    public UserApiController(UserClient userClient) {
        this.userClient = userClient;
    }

    @ApiOperation("获取微信openid信息")
    @GetMapping("getWeixinOpenid")
    public Result getWeixinOpenid(String js_code) {
        try {
            return userClient.getWeixinOpenid(js_code);
        } catch (Exception e) {
            return Result.fail("得到openid失败");
        }
    }

    @GetMapping("getUser")
    @ApiOperation("得到用户信息")
    public Result getUser(String openid) {
        return userClient.getUser(openid);
    }

    @GetMapping("getUserStatistics")
    @ApiOperation("得到用户信息")
    public Result getUserStatistics(String openid) throws WxErrorException {
        return userClient.getUserStatistics(openid);
    }

    @PostMapping("saveUser")
    @ApiModelProperty("保存用户信息")
    public Result saveUser(@RequestBody MemberUserRequest userRequest) {
        userRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
        userRequest.setDelFlag(DelFlagEnum.NO.getValue());
        return userClient.saveUser(userRequest);
    }
    
    @PostMapping("updateCity")
    @ApiModelProperty("更新城市")
    public Result updateCity(@RequestBody AdminRequest request) {
        return userClient.updateCity(request.getId(), request.getCity());
    }

    /**
     * 用户注册
     *
     * @param adminRequest
     * @return
     */
    @PostMapping("registerUser")
    public Result registerUser(@RequestBody AdminRequest adminRequest) {
        return userClient.registerUser(adminRequest);
    }

    /**
     * 上传图像
     *
     * @param file
     * @return com.retail.core.entity.result.Result
     * @date: 2024/6/5 22:51
     */
    @PostMapping("uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile file) {
        return userClient.uploadImage(file);
    }

    /**
     * 更新用户信息
     * @param adminRequest
     * @return
     */
    @PostMapping("updateUserInfo")
    public Result updateUserInfo(@RequestBody  AdminRequest adminRequest) {
        adminRequest.setAuditStatus(null);
        adminRequest.setDeptIds(null);
        adminRequest.setRoleIds(null);
        adminRequest.setStatusFlag(null);
        adminRequest.setPass(null);
        adminRequest.setPhone(null);
        return userClient.updateUserInfo(adminRequest);
    }

    @ApiModelProperty("用户统计数据")
    @GetMapping("userstastics/{adminId}")
    public Result<SysAdminVerifyStasticResVo> userstastics(@PathVariable("adminId") Long adminId) {
        return userClient.userstastics(adminId);
    }

    @ApiModelProperty("团队数据")
    @GetMapping("teams/{adminId}")
    public Result<RetailMultiTeamResVo> teams(@PathVariable("adminId") Long adminId){
        return userClient.teams(adminId);
    }

    @ApiModelProperty("我的团队数据")
    @GetMapping("myteams/{adminId}")
    public Result<List<AdminVo>> myteams(@PathVariable("adminId") Long adminId){
        return userClient.myteams(adminId);
    }

    @ApiModelProperty("团队数据")
    @GetMapping("teamDetail/{adminId}")
    public Result<RetailTeamResVo> teamDetail(@PathVariable("adminId") Long adminId) {
        return userClient.teamDetail(adminId);
    }


    @ApiModelProperty("认证")
    @PostMapping("authInfo/{adminId}")
    public Result<RetailTeamResVo> authInfo(@PathVariable("adminId") Long adminId, @RequestBody JSONObject jsonObject) {
        return userClient.authInfo(adminId, jsonObject);
    }

    @ApiModelProperty("没有上级的用户")
    @GetMapping("noParentIdAdmin")
    public Result<List<AdminVo>> noParentIdAdmin() {
        return userClient.noParentIdAdmin();
    }

    @ApiModelProperty("添加团队审核")
    @PostMapping("saveTeamAddSp")
    public Result<Integer> saveTeamAddSp(@RequestBody RetailTeamAddSpRequest request){
        return userClient.saveTeamAddSp(request);
    }

    @ApiModelProperty("申请代理")
    @PostMapping("applyPorxy")
    public Result<Integer> applyPorxy(@RequestBody SysAdminApplyProxyRequest request) {
        return userClient.applyPorxy(request);
    }
}
